﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace winformLesson1
{
    public partial class UserLogin : Form
    {
        public UserLogin()
        {
            InitializeComponent();
        }

        private void UserLogin_Load(object sender, EventArgs e)
        {

        }

        private void btn_UserLogin_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("你好"); // 卡UI线程，后面代码都不会执行

            // 1、采集数据
            string username = tbx_UserName.Text.Trim();
            string userpwd = tbx_UserPwd.Text; //不去前后空格，输入的空格也算是密码的一部分

            // 2、判断账号密码是否为空
            if (string.IsNullOrEmpty(username))
            {
                MessageBox.Show("账号不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(userpwd))
            {
                MessageBox.Show("密码不能为空！");
                return;
            }

            // 3、在判断账号密码正确之前先判断账号是否存在
            if (UserNameIsExist(username) == false)
            {
                MessageBox.Show("用户不存在，请先注册！");
                return;
            }

            // 3、校验账号密码是否正确
            int userid = Login(username, userpwd);
            if (userid<=0)
            {
                MessageBox.Show("账号密码错误");
                return;
            }

            // 登录成功 做的事情
            Main main = new Main(userid);
            main.Show();
            this.Hide();
        }



        public bool UserNameIsExist(string username)
        {
            string sql = string.Format("SELECT count(UserId) FROM [User] where UserName=@UserName");
            SqlParameter[] paras = new SqlParameter[] 
            {
                new SqlParameter("@UserName",username)
            };
            return SqlHelper.Exist(sql, paras);

            // 1、准备连接字符串
            // string connStr = "server=DESKTOP-111053T\\MSSQL; database=WebTest; Uid=sa; Pwd=598642004;";
            //string connStr = ConfigurationManager.AppSettings["connStr"];


            // 2、准备连接对象 SqlConnection
            //int r = 0;

            //using (SqlConnection conn = new SqlConnection(connStr))
            //{
            //    // 3、准备sql 语句
            //    string sql = string.Format("SELECT count(UserId) FROM [User] where UserName=@UserName");
            //    // 4、 创建 SqlCommand 对象
            //    using (SqlCommand comm = new SqlCommand(sql, conn))
            //    {
            //        comm.Parameters.Add(new SqlParameter("@UserName", username));
            //        conn.Open();
            //        r = (int)comm.ExecuteScalar();
            //    }
            //}
            //return r == 0 ? false:true;
        }

        public int Login(string username, string userpwd)
        {
            
            string sql = string.Format("SELECT UserId FROM [User] where UserName=@UserName and UserPwd=@UserPwd");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@UserName", username),
                new SqlParameter("@UserPwd", userpwd)
            };
            int r = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, paras) ?? "0");
            return r > 0 ? r : 0;

            //// 1、准备连接字符串
            ////string connStr = "server=DESKTOP-111053T\\MSSQL; database=WebTest; Uid=sa; Pwd=598642004;";
            //string connStr = ConfigurationManager.AppSettings["connStr"];


            //// 2、准备连接对象 SqlConnection
            //int r = 0;

            //using (SqlConnection conn = new SqlConnection(connStr))
            //{
            //    // 3、准备sql 语句
            //    string sql = string.Format("SELECT UserId FROM [User] where UserName=@UserName and UserPwd=@UserPwd");
            //    // 4、 创建 SqlCommand 对象
            //    using (SqlCommand comm = new SqlCommand(sql, conn))
            //    {
            //comm.Parameters.Add(new SqlParameter("@UserName", username));
            //comm.Parameters.Add(new SqlParameter("@UserPwd", userpwd));
            //        conn.Open();
            //        r = (int)comm.ExecuteScalar();
            //    }
            //}

            //return r > 0 ? r : 0;
        }


        public void LoginOld(string username, string userpwd)
        {
            // 1、准备连接字符串
            //string connStr = "server=DESKTOP-111053T\\MSSQL; database=WebTest; Uid=sa; Pwd=598642004;";
            string connStr = ConfigurationManager.AppSettings["connStr"];


            // 2、准备连接对象 SqlConnection
            SqlConnection conn = new SqlConnection(connStr);
            int r = 0;
            try
            {
                conn.Open();
                // 3、准备sql 语句
                string sql = string.Format("SELECT count(UserId) FROM [User] where UserName='{0}' and UserPwd='{1}'",username,userpwd);

                // 4、 创建 SqlCommand 对象
                SqlCommand comm = new SqlCommand(sql,conn);
                 r = (int)comm.ExecuteScalar();
                
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接失败");
            }
            finally
            {
                conn.Dispose();
                conn.Close();
            }

            if (r == 1)
            {
                MessageBox.Show("登录成功");
            }
            else
            {
                MessageBox.Show("登录失败");
            }

        }

        private void btn_Reg_Click(object sender, EventArgs e)
        {
            Register register = new Register();
            register.Show();
        }
    }
}
